package com.huifaapp.mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.JDcomclassifyAdapter;
import com.huifaapp.mall.adapter.Tmall_popwinAdapter;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.JDcomclassifyBean;
import com.huifaapp.mall.fragment.TmallClassifyBaseFragment;
import com.huifaapp.mall.uitils.Contig;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by duanyu on 2018/1/30.
 */

public class Tmall_homepage2Fragment extends Fragment {
    public static Map<Integer, Boolean> hmap;
    private View mview;
    private TabLayout tmall_tablayout;
    private ViewPager tmall_viewPager;
    private ImageView tmall_popupWindow;
    private FloatingActionButton floatingActionButton;
    private FloatingActionButton dialog_tmall_floatingActionButton_share;
    private List<JDcomclassifyBean.DataBean> classif;
    private  String EATEGORYSURL= ApiManager.BASE_NIUPAI_URL+ Contig.typex+"/categorys";//小分类
    private List<TmallClassifyBaseFragment> tmallClassifyBaseFragments = new ArrayList<>();
    private JDcomclassifyBean dataBean;
    private CharSequence transitionName;
    private List<Fragment> fragmentLIST;
    private String acd;

    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        try{
        if (mview == null) {
            mview = inflater.inflate(R.layout.fragment_tamll_homepag2, container, false);
        }
        hmap = new HashMap<>();
        initView();
        inittabHttp();
    }catch (Exception e){
        Log.e("Exception",e.getMessage());
    }
        return mview;
    }

    private void initView() {
        try {
            ImageView i1 = (ImageView) mview.findViewById(R.id.i1);
            TextView i3 = (TextView) mview.findViewById(R.id.i3);
            ImageView i2 = (ImageView) mview.findViewById(R.id.i2);
            Log.e("类型", "qqssqq" + Contig.typex);
            if (Contig.typex.equals("yh")) {
                i3.setText("京选友惠");
            } else {
                if (Contig.typex.equals("jd")) {
                    i3.setText("京东精选");
                }
            }
            //返回
            i1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  getActivity(). finish();
                }
            });

            //搜索
            i2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), SearActivity.class);
                    startActivity(intent);
                }
            });

            tmall_tablayout = (TabLayout) mview.findViewById(R.id.tmall_tablayout);
            tmall_viewPager = (ViewPager) mview.findViewById(R.id.tmall_ViewPager);
            tmall_popupWindow = (ImageView) mview.findViewById(R.id.tmall_PopupWindow);
            tmall_popupWindow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    View contentView = View.inflate(getActivity(), R.layout.popwin_supplier_gite, null);
                    RecyclerView popwin_supplier_gite_gridView = (RecyclerView) contentView.findViewById(R.id.popwin_supplier_gite_gridView);
                    final Tmall_popwinAdapter Tmall_popwinAdapter = new Tmall_popwinAdapter(classif, getActivity());
                    popwin_supplier_gite_gridView.setLayoutManager(new GridLayoutManager(getActivity(), 4, GridLayoutManager.VERTICAL, false));
                    popwin_supplier_gite_gridView.setAdapter(Tmall_popwinAdapter);
                    Tmall_popwinAdapter.setmMyItemclickListener(new Tmall_popwinAdapter.MypopItemclickListener() {
                        @Override
                        public void itemclick(View view, final int position) {
                            tmall_viewPager.setCurrentItem(position);
                            tmall_tablayout.getTabAt(position).select();
                            Contig.categroyId = classif.get(position).getId();
                            Contig.categrouCode=classif.get(position).getCodeid();
                                //改变选中状态
                            Tmall_popwinAdapter.setCurrentItem(position);
                                //通知ListView改变状态
                            Tmall_popwinAdapter.notifyDataSetChanged();
                        }
                    });
                    PopupWindow mPopupMenu = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    mPopupMenu.setContentView(contentView);
                    mPopupMenu.setOutsideTouchable(true);
                    //是popuwindow里填充的listView拥有焦点
                    mPopupMenu.setFocusable(true);
                    View rootview = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_tmall, null);
                    mPopupMenu.showAsDropDown(tmall_tablayout);
                    // mPopupMenu.showAtLocation(rootview, Gravity.AXIS_PULL_BEFORE, 0,-460);
                }
            });
        }catch (Exception e){}
    }

    //小分类网络请求
    private void inittabHttp() {
        try {
            fragmentLIST = new ArrayList<>();
            OkHttpUtils
                    .get()
                    .url(EATEGORYSURL)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                        }

                        @Override
                        public void onResponse(String response) {
                            Log.e("小分类", response);
                            Gson json = new Gson();
                            classif = new ArrayList<>();

                            dataBean = json.fromJson(response, JDcomclassifyBean.class);
                            if (dataBean.getCode().equals("10000")) {
                                classif.addAll(dataBean.getData());
                                for (JDcomclassifyBean.DataBean dataBean1 : classif) {

                                    if (dataBean1.getName().equals("全部") || dataBean1.getName().equals("推荐")) {
                                        try {
                                            TmallhomepageFragment tmallhomepageFragment = new TmallhomepageFragment();
                                            tmallhomepageFragment.setFragmentTitle(dataBean1.getName());
                                            tmallClassifyBaseFragments.add(tmallhomepageFragment);
                                        } catch (Exception e) {

                                        }

                                    } else {
                                        try {
//                                Contig.categroyId=dataBean1.getId();
                                            TmallClassifyWithSPBJ tmallClassifyWithSPBJ = new TmallClassifyWithSPBJ();
                                            tmallClassifyWithSPBJ.setFragmentTitle(dataBean1.getName());
//                                            Contig.categrouCode=dataBean1.getCodeid();
                                            tmallClassifyBaseFragments.add(tmallClassifyWithSPBJ);
                                            fragmentLIST.add(tmallClassifyWithSPBJ);
                                        } catch (Exception e) {
                                        }
                                    }
                                }

                                JDcomclassifyAdapter JDcomclassifyAdapter = new JDcomclassifyAdapter(getChildFragmentManager(), tmallClassifyBaseFragments);
                                tmall_viewPager.setAdapter(JDcomclassifyAdapter);
                                tmall_tablayout.setupWithViewPager(tmall_viewPager);
                                tmall_tablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                                tmall_tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                                    @Override
                                    public void onTabSelected(TabLayout.Tab tab) {
                                        for (int i = 0; i < classif.size(); i++) {
                                            if (classif.get(i).getName().equals(tab.getText())) {
                                                Contig.categroyId = classif.get(i).getId();
                                                Contig.categrouCode=classif.get(i).getCodeid();
                                                Contig.tabname=classif.get(i).getName();
                                            }
                                        }

//                                Toast.makeText(getActivity(), "选中的"+tab.getText(), Toast.LENGTH_SHORT).show();
                                    }

                                    @Override
                                    public void onTabUnselected(TabLayout.Tab tab) {

//                                Toast.makeText(getActivity(), "未选中的"+tab.getText(), Toast.LENGTH_SHORT).show();
                                    }

                                    @Override
                                    public void onTabReselected(TabLayout.Tab tab) {

//                                Toast.makeText(getActivity(), "复选的"+tab.getText(), Toast.LENGTH_SHORT).show();

                                    }
                                });
                            }else{
                                Toast.makeText(getActivity(),dataBean.getMsg(),Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

        }catch (Exception e){}
    }
}
